<template>
  <div id="add-gateway-env">
    <el-form :model="gwInfoForm" :rules="rules" ref="ruleForm" label-width="140px" class="demo-ruleForm">
      <el-row>
        <el-col :span=12>
          <el-form-item label="网关编号:" prop="gwEnvCode">
            <el-input v-model="gwInfoForm.gwEnvCode"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span=12>
          <el-form-item label="网关名称:" prop="gwEnvName">
            <el-input v-model="gwInfoForm.gwEnvName"></el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span=12>
          <el-form-item label="网关外部地址:" prop="gwEnvUrl">
            <el-input v-model="gwInfoForm.gwEnvUrl"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span=12>
          <el-form-item label="网关部署地址:" prop="gwEnvAddress">
            <el-input v-model="gwInfoForm.gwEnvAddress"></el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span=12>
          <el-form-item label="网关服务状态:" prop="gwEnvStatus">
            <el-select v-model="gwInfoForm.gwEnvStatus" placeholder="请选择" popper-class="zIndex-popper">
              <el-option
                v-for="item in gwServeStatus"
                :key="item.value"
                :label="item.label"
                :value="item.value">
              </el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span=12>
          <el-form-item label="网关类型:" prop="gwtype">
            <el-select v-model="gwInfoForm.gwtype" placeholder="请选择" popper-class="zIndex-popper">
              <el-option
                v-for="item in gwTypes"
                :key="item.value"
                :label="item.label"
                :value="item.value">
              </el-option>
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span=12>
          <el-form-item label="选择证书:" prop="keyid">
            <el-select v-model="gwInfoForm.keyid" placeholder="请选择" popper-class="zIndex-popper">
              <el-option
                v-for="item in certific"
                :key="item.value"
                :label="item.label"
                :value="item.value">
              </el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span=12>
          <el-form-item label="网关环境描述:" prop="gwEnvDesc">
            <el-input type="textarea" v-model="gwInfoForm.gwEnvDesc"></el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="24">
        <el-col>
          <el-button @click="clearForm">重置</el-button>
          <el-button @click="cancel">关闭</el-button>
          <el-button type="primary" @click="submit('ruleForm')">确定</el-button>
        </el-col>
      </el-row>
    </el-form>
  </div>
</template>
<script>
import api from '../../../../../api/url'
import ajax from '../../../../../api/ajax/ajax'
import Msg from '../../../../../assets/js/msg'
export default {
  data () {
    return {
      gwServeStatus: [{ // 网关服务状态
        value: '00',
        label: '开放服务'
      }, {
        value: '01',
        label: '停止服务'
      }],
      certific: [], // 选择证书
      gwTypes: [{ // 网关类型
        value: '00',
        label: '开放'
      }, {
        value: '01',
        label: '沙箱'
      }, {
        value: '02',
        label: '内联'
      }],
      gwInfoForm: {
        gwEnvAddress: '',
        gwEnvCode: '',
        gwEnvDesc: '',
        gwEnvName: '',
        gwEnvStatus: '',
        gwEnvUrl: '',
        gwtype: '',
        keyid: ''
      },
      rules: {
        gwEnvCode: [
          { required: true, message: '请输入网关编号', trigger: 'blur' }
        ],
        gwEnvName: [
          { required: true, message: '请输入网关名称', trigger: 'blur' }
        ],
        gwEnvUrl: [
          { required: true, message: '请输入网关外部地址', trigger: 'blur' }
        ],
        gwEnvAddress: [
          { required: true, message: '请输入网关部署地址', trigger: 'blur' }
        ],
        gwEnvStatus: [
          { required: true, message: '请选择服务状态', trigger: 'change' }
        ],
        gwEnvDesc: [
          { required: true, message: '请输入网关环境描述', trigger: 'blur' }
        ],
        keyid: [
          { required: true, message: '请选择证书', trigger: 'change' }
        ],
        gwtype: [
          { required: true, message: '请选择网关类型', trigger: 'change' }
        ]
      }
    }
  },
  methods: {
    // tab切换 清空表单&&验证
    clearForm () {
      this.$refs.ruleForm.resetFields()
    },
    submit (formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          ajax.post(api.ADD_GW_ENV, this.gwInfoForm, true).then(() => {
            this.$emit('refresh')
            this.$message({
              type: 'success',
              message: '添加成功'
            })
            // this.$refs.ruleForm.resetFields()
            this.$parent.$parent.addGatewayCancel()
          }).catch(err => {
            Msg.alert(err.errorMsg)
          })
        }
      })
    },
    cancel () {
      this.$parent.$parent.addGatewayCancel()
    }
  },
  mounted () {
    this.$on('getAddIdDat', ({list}) => {
      // 对请求的网关证书信息进行处理
      let newData = list.map(item => {
        let newIdObj = {
          label: item.keycode,
          value: item.KeyId
        }
        return newIdObj
      })
      this.certific = newData
    })
  },
  created () {
  }
}
</script>
<style lang="stylus" rel="stylesheet/stylus">
  #add-gateway-env
    background #ffffff
    padding-bottom 10px
    .w150
      display inline-block
      width 150px
    .title-pad20
      height: 40px
      line-height 40px
      padding-left 20px
    .el-form-item__label
      width 120px
    .el-form-item
      margin-bottom 10px
    .el-input
      width 100%
    .el-select
    .el-input
      width 100%
    .el-textarea
      width 100%
    .el-col-24
      text-align right
      margin-top 35px
      padding-right 35px !important
    .el-col-12
      padding 0 11px
    .el-form-item
      margin-bottom 22px
</style>
